package com.edais.actions;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.edais.domain.User;
import com.edais.service.ErrorMessageService;
import com.edais.service.LuckDrawService;
import com.edais.service.UserService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.JsonUtil;
import com.edais.util.Message;
import com.edais.util.StringUtil;

@Controller
@RequestMapping("/luckdraw")
public class LuckDrawAction {

	private static final Log logger = LogFactory.getLog(LuckDrawAction.class);

	@Autowired
	private LuckDrawService luckDrawService;

	@Autowired
	private UserService userService;

	@Autowired
	private ErrorMessageService errorMessageService;

	

	/**
	 * @Description: 查询用户特殊抽奖表信息
	 * @param @param request
	 * @param @param response
	 * @return void
	 * @throws
	 * @author chenshufeng
	 * @date 2016-9-2
	 */
	@RequestMapping(value = "/showSpecLuckDraw")
	public void showSpecLuckDraw(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> jsonMap = new TreeMap<String, Object>();
		// 解密请求参数
		try {
			Map<String, String> paramsMap = CommonUtil
					.decryptParamters(request);
			logger.info("查询用户抽奖表信息参数："+paramsMap);
			if (null == paramsMap ||  StringUtil.isEmpty(paramsMap.get("mobile"))) {
				//获取特殊奖项列表
				Map<String, String> reqMap = new HashMap<String, String>();
				reqMap.put("userId", "");
				jsonMap = luckDrawService.getSpecGiftsList(reqMap);
				logger.info("特殊奖项列表:"+jsonMap);
				String result = JsonUtil.getJsonStringFromMap(jsonMap);
				CommonUtil.responseJson(result, response);
				return;
			}
//			if (StringUtil.isEmpty(paramsMap.get("channel"))) {
//				logger.info("channel is null");
//				String result = CommonUtil.setResultStringCn(
//						new HashMap<String, Object>(), Consts.CHECK_CODE, "", Message.CnDescriptionExpression.BUSINESSE_ERROR);
//				CommonUtil.responseJson(result, response);
//				return;
//			}
			Map<String, Object> userMap = new HashMap<String, Object>();
			userMap.put("mobile", paramsMap.get("mobile"));
			User user = userService.getUserByParm(userMap);
			if(null == user){
				logger.info("user is null");
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(), Consts.CHECK_CODE, "", "抱歉，您没有获得抽奖次数");
				CommonUtil.responseJson(result, response);
				return;
			}
			String userId = String.valueOf(user.getId());

			if (StringUtil.isEmpty(userId)) {
				logger.info("userId is null");
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(), Consts.CHECK_CODE, "", Message.CnDescriptionExpression.BUSINESSE_ERROR);
				CommonUtil.responseJson(result, response);
				return;
			}
			//获取特殊奖项列表
			Map<String, String> reqMap = new HashMap<String, String>();
			reqMap.put("userId", userId);
			jsonMap = luckDrawService.getSpecGiftsList(reqMap);
			logger.info("特殊奖项列表:"+jsonMap);
			// 处理返回结果
			String result = JsonUtil.getJsonStringFromMap(jsonMap);
			CommonUtil.responseJson(result, response);
		} catch (Exception e) {
			logger.error("查询用户抽奖表信息:",e);
			String result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}
	
	/**
	 * @Description: 特殊抽奖
	 * @param @param request
	 * @param @param response
	 * @return void
	 * @throws
	 * @author chenshufeng
	 * @date 2016-9-2
	 */
	@RequestMapping(value = "/userSpecLuckDraw")
	public void userSpecLuckDraw(HttpServletRequest request,
			HttpServletResponse response) {
		// 解密请求参数
		try {
			Map<String, String> paramsMap = CommonUtil
					.decryptParamters(request);
			logger.info("用户抽奖参数："+paramsMap);
			if (null == paramsMap ||  StringUtil.isEmpty(paramsMap.get("mobile"))) {
				logger.info("mobile is null");
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(), Consts.CHECK_CODE, "", Message.CnDescriptionExpression.BUSINESSE_ERROR);
				CommonUtil.responseJson(result, response);
				return;
			}
//			if (StringUtil.isEmpty(paramsMap.get("channel"))) {
//				logger.info("channel is null");
//				String result = CommonUtil.setResultStringCn(
//						new HashMap<String, Object>(), Consts.CHECK_CODE, "", Message.CnDescriptionExpression.BUSINESSE_ERROR);
//				CommonUtil.responseJson(result, response);
//				return;
//			}
			Map<String, Object> userMap = new HashMap<String, Object>();
			userMap.put("mobile", paramsMap.get("mobile"));
			User user = userService.getUserByParm(userMap);
			if(null == user){
				logger.info("user is null");
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(), Consts.CHECK_CODE, "", "抱歉，您没有获得抽奖次数");
				CommonUtil.responseJson(result, response);
				return;
			}
			String userId = String.valueOf(user.getId());
			if (StringUtil.isEmpty(userId)) {
				logger.info("userId is null");
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(), Consts.CHECK_CODE, "", Message.CnDescriptionExpression.BUSINESSE_ERROR);
				CommonUtil.responseJson(result, response);
				return;
			}
			// 1-是否有抽奖资格
			Map<String, String> map = luckDrawService.isExistLuckDrawTimes(userId);
			if (!"00000".equals(map.get("rescode"))) {
				logger.info("抽奖次数没有了！");
				Map<String, Object> erromMap = errorMessageService
						.getErrorMessage("SPEC_LUCK_DRAW_TIMES_WARN");
				erromMap.put("rescode", Consts.ERROR_CODE);
				erromMap.put("resmsg_cn", "");
				String result = JsonUtil.getJsonStringFromMap(erromMap);
				CommonUtil.responseJson(result, response);
				return;
			}
			// 2-随机抽奖奖品
			int giftId = luckDrawService.getRandomGiftNo();
			if(giftId == -1){
				logger.info("================所有奖品库存都没有了!");
				Map<String, Object> erromMap = errorMessageService
						.getErrorMessage("SPEC_LUCK_DRAW_STORE_WARN");
				erromMap.put("rescode", Consts.ERROR_CODE);
				erromMap.put("resmsg_cn", "");
				String result = JsonUtil.getJsonStringFromMap(erromMap);
				CommonUtil.responseJson(result, response);
				return;
			}else if(giftId == -2||giftId == -3){
				logger.info("================有库存的奖项的总概率为0或者概率配置出错!");
				Map<String, Object> erromMap = errorMessageService
						.getErrorMessage("SPEC_LUCK_DRAW_ZERO");
				erromMap.put("rescode", Consts.ERROR_CODE);
				erromMap.put("resmsg_cn", "");
				String result = JsonUtil.getJsonStringFromMap(erromMap);
				CommonUtil.responseJson(result, response);
				return;
			}
			logger.info("================随机抽奖奖品Id为：" + giftId);
			// 3-送奖品
			Map<String, String> pmap = new HashMap<String, String>();
			pmap.put("giftId", String.valueOf(giftId));
			pmap.put("userId", userId);
			pmap.put("cid", (String) map.get("cid"));
			Map<String, Object> luckMap = luckDrawService.sendGift(pmap);
			if (!"00000".equals(luckMap.get("rescode"))) {
				logger.info("送奖品失败！");
				Map<String, Object> erromMap = errorMessageService
						.getErrorMessage("BUSINESSE_ERROR");
				erromMap.put("rescode", Consts.ERROR_CODE);
				erromMap.put("resmsg_cn", "");
				String result = JsonUtil.getJsonStringFromMap(erromMap);
				CommonUtil.responseJson(result, response);
				return;
			}
			luckMap.put("restLuckDrawTimes",
					Integer.valueOf(map.get("restLuckDrawTimes")) - 1);
			//处理返回结果
			String result = JsonUtil.getJsonStringFromMap(luckMap);
			CommonUtil.responseJson(result, response);
		} catch (Exception e) {
			logger.error("抽奖异常:",e);
			String result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(), Consts.ERROR_CODE,
					Consts.ErrorMsg.MSG00001,
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}
	
	/**
	 * @Description: 双旦奖项列表
	 * @param @param request
	 * @param @param response   
	 * @return void  
	 * @throws
	 * @author chenshufeng
	 * @date 2016-12-8
	 */
	@RequestMapping(value = "/showSDLuckDraw")
	public void showSDLuckDraw(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> jsonMap = new TreeMap<String, Object>();
		// 解密请求参数
		try {
			Map<String, String> paramsMap = CommonUtil
					.decryptParamters(request);
			logger.info("查询用户双旦抽奖表信息参数："+paramsMap);
			if (null == paramsMap ||  StringUtil.isEmpty(paramsMap.get("mobile"))) {
				logger.info("mobile is null");
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(), Consts.CHECK_CODE, "", "对不起,请您登录后再参加砸蛋！");
				CommonUtil.responseJson(result, response);
				return;
//				//获取双旦奖项列表
//				Map<String, String> reqMap = new HashMap<String, String>();
//				reqMap.put("userId", "");
//				jsonMap = luckDrawService.getSDGiftsList(reqMap);
//				logger.info("双旦奖项列表:"+jsonMap);
//				String result = JsonUtil.getJsonStringFromMap(jsonMap);
//				CommonUtil.responseJson(result, response);
//				return;
			}
			Map<String, Object> userMap = new HashMap<String, Object>();
			userMap.put("mobile", paramsMap.get("mobile"));
			User user = userService.getUserByParm(userMap);
			if(null == user){
				logger.info("user is null");
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(), Consts.CHECK_CODE, "", "抱歉，您没有获得抽奖次数");
				CommonUtil.responseJson(result, response);
				return;
			}
			String userId = String.valueOf(user.getId());
			
			if (StringUtil.isEmpty(userId)) {
				logger.info("userId is null");
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(), Consts.CHECK_CODE, "", "对不起,请您登录后再参加砸蛋！");
				CommonUtil.responseJson(result, response);
				return;
			}
			//获取特殊奖项列表
			Map<String, String> reqMap = new HashMap<String, String>();
			reqMap.put("userId", userId);
			jsonMap = luckDrawService.getSDGiftsList(reqMap);
			logger.info("双旦奖项列表:"+jsonMap);
			// 处理返回结果
			String result = JsonUtil.getJsonStringFromMap(jsonMap);
			CommonUtil.responseJson(result, response);
		} catch (Exception e) {
			logger.error("查询用户双旦抽奖表信息:",e);
			String result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}
	
	/**
	 * @Description: 双旦抽奖
	 * @param @param request
	 * @param @param response   
	 * @return void  
	 * @throws
	 * @author chenshufeng
	 * @date 2016-12-8
	 */
	@RequestMapping(value = "/userSDLuckDraw")
	public void userSDLuckDraw(HttpServletRequest request,
			HttpServletResponse response) {
		// 解密请求参数
		try {
			logger.info("开始抽奖=================");
			Map<String, String> paramsMap = CommonUtil
					.decryptParamters(request);
			logger.info("用户双旦抽奖参数："+paramsMap);
			if (null == paramsMap ||  StringUtil.isEmpty(paramsMap.get("mobile"))) {
				logger.info("用户双旦抽奖参数 mobile is null");
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(), Consts.CHECK_CODE, "", Message.CnDescriptionExpression.BUSINESSE_ERROR);
				CommonUtil.responseJson(result, response);
				return;
			}
			if (StringUtil.isEmpty(paramsMap.get("drawFlag"))) {
				logger.info("用户双旦抽奖参数 drawFlag is null");
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(), Consts.CHECK_CODE, "", Message.CnDescriptionExpression.BUSINESSE_ERROR);
				CommonUtil.responseJson(result, response);
				return;
			}
			Map<String, Object> userMap = new HashMap<String, Object>();
			userMap.put("mobile", paramsMap.get("mobile"));
			User user = userService.getUserByParm(userMap);
			if(null == user){
				logger.info("user is null");
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(), Consts.CHECK_CODE, "", "抱歉，您没有获得抽奖次数");
				CommonUtil.responseJson(result, response);
				return;
			}
			String userId = String.valueOf(user.getId());
			if (StringUtil.isEmpty(userId)) {
				logger.info("userId is null");
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(), Consts.CHECK_CODE, "", Message.CnDescriptionExpression.BUSINESSE_ERROR);
				CommonUtil.responseJson(result, response);
				return;
			}
			String drawFlag = paramsMap.get("drawFlag");
			// 1-是否有抽奖资格
			logger.info("抽奖资格=================");
			Map<String, String> map = luckDrawService.isExistSDLuckDrawTimes(userId,drawFlag);
			if (!"00000".equals(map.get("rescode"))) {
				logger.info("抽奖次数没有了！");
				Map<String, Object> erromMap = errorMessageService
						.getErrorMessage("SD_LUCK_DRAW_TIMES_WARN");
				erromMap.put("rescode", Consts.ERROR_CODE);
				erromMap.put("resmsg_cn", "");
				String result = JsonUtil.getJsonStringFromMap(erromMap);
				CommonUtil.responseJson(result, response);
				return;
			}
			// 2-随机抽奖奖品
			Map<String, Object> reqMap = new HashMap<String, Object>();
			if (Integer.valueOf(drawFlag) == 0) {
				reqMap.put("drawSet", 1);// 首次抽奖
			} else {
				reqMap.put("drawSet", 2);
			}
			int giftId = luckDrawService.getRandomSDGiftNo(reqMap);
			logger.info("抽奖ID=================" + giftId);
			if (giftId == -1) {
				logger.info("================所有奖品库存都没有了!");
				Map<String, Object> erromMap = errorMessageService
						.getErrorMessage("SD_LUCK_DRAW_STORE_WARN");
				erromMap.put("rescode", Consts.ERROR_CODE);
				erromMap.put("resmsg_cn", "");
				String result = JsonUtil.getJsonStringFromMap(erromMap);
				CommonUtil.responseJson(result, response);
				return;
			} else if (giftId == -2 || giftId == -3) {
				logger.info("================有库存的奖项的总概率为0或者概率配置出错!");
				Map<String, Object> erromMap = errorMessageService
						.getErrorMessage("SD_LUCK_DRAW_ZERO");
				erromMap.put("rescode", Consts.ERROR_CODE);
				erromMap.put("resmsg_cn", "");
				String result = JsonUtil.getJsonStringFromMap(erromMap);
				CommonUtil.responseJson(result, response);
				return;
			}
			logger.info("================随机抽奖奖品Id为：" + giftId);
			// 3-送奖品
			logger.info("赠送奖品开始=========================");
			Map<String, String> pmap = new HashMap<String, String>();
			pmap.put("giftId", String.valueOf(giftId));
			pmap.put("userId", userId);
			pmap.put("mobile", paramsMap.get("mobile"));
			pmap.put("cid", (String) map.get("cid"));
			pmap.put("drawFlag", drawFlag);

			Map<String, Object> luckMap = luckDrawService.sendSDGift(pmap);
			if (!"00000".equals(luckMap.get("rescode"))) {
				logger.info("送奖品失败！");
				Map<String, Object> erromMap = errorMessageService
						.getErrorMessage("BUSINESSE_ERROR");
				erromMap.put("rescode", Consts.ERROR_CODE);
				erromMap.put("resmsg_cn", "");
				String result = JsonUtil.getJsonStringFromMap(erromMap);
				CommonUtil.responseJson(result, response);
				return;
			}
			luckMap.put("restLuckDrawTimes",
					Integer.valueOf(map.get("restLuckDrawTimes")) - 1);
			//处理返回结果
			String result = JsonUtil.getJsonStringFromMap(luckMap);
			CommonUtil.responseJson(result, response);
		} catch (Exception e) {
			logger.error("双旦抽奖异常:",e);
			String result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(), Consts.ERROR_CODE,
					Consts.ErrorMsg.MSG00001,
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}
	
	/**
	 * @Description: 中奖记录
	 * @param @param request
	 * @param @param response   
	 * @return void  
	 * @throws
	 * @author chenshufeng
	 * @date 2016-12-12
	 */
	@RequestMapping(value = "/getSDLuckDrawResult")
	public void getSDLuckDrawResult(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> jsonMap = new TreeMap<String, Object>();
		// 解密请求参数
		try {
			Map<String, String> paramsMap = CommonUtil
					.decryptParamters(request);
			logger.info("查询用户双旦抽奖记录信息参数："+paramsMap);
			if (null == paramsMap ||  StringUtil.isEmpty(paramsMap.get("mobile"))) {
				logger.info("mobile is null");
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(), Consts.CHECK_CODE, "", Message.CnDescriptionExpression.BUSINESSE_ERROR);
				CommonUtil.responseJson(result, response);
				return;
			}
			Map<String, Object> userMap = new HashMap<String, Object>();
			userMap.put("mobile", paramsMap.get("mobile"));
			User user = userService.getUserByParm(userMap);
			if(null == user){
				logger.info("user is null");
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(), Consts.CHECK_CODE, "", Message.CnDescriptionExpression.BUSINESSE_ERROR);
				CommonUtil.responseJson(result, response);
				return;
			}
			String userId = String.valueOf(user.getId());
			
			if (StringUtil.isEmpty(userId)) {
				logger.info("userId is null");
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(), Consts.CHECK_CODE, "", Message.CnDescriptionExpression.BUSINESSE_ERROR);
				CommonUtil.responseJson(result, response);
				return;
			}
			//获取抽奖记录
			Map<String, Object> parms = new HashMap<String, Object>();
			// 默认页
			Integer current = 1;
			Integer pageSize = 10;
			String currentStr = String.valueOf(paramsMap.get(Consts.Page.PAGE_CURRENT_NAME)); // 当前页数
			String pageSizeStr = String.valueOf(paramsMap.get(Consts.Page.PAGE_SIZE_NAME)); // 页大小
			if (StringUtil.isNotEmpty(currentStr)) {
				current = Integer.parseInt(currentStr);
			}
			if (StringUtil.isNotEmpty(pageSizeStr)) {
				pageSize = Integer.parseInt(pageSizeStr);
			}
			parms.put("beginRow", (current - 1) * pageSize);
			parms.put("endRow", pageSize);
			parms.put("userId", userId);
			jsonMap = luckDrawService.getSDLuckDrawResult(parms);
			logger.info("双旦抽奖记录列表:"+jsonMap);
			// 处理返回结果
			String result = JsonUtil.getJsonStringFromMap(jsonMap);
			CommonUtil.responseJson(result, response);
		} catch (Exception e) {
			logger.error("查询用户双旦抽奖记录:",e);
			String result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(), Consts.ERROR_CODE, "",
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}
}
